package com.taichuan.selfcheck.mvp.presenter;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import com.taichuan.mvp.base.MvpBasePresenter;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.entity.WifiBean;
import com.taichuan.selfcheck.mvp.viewinterface.WifiInterface;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class WifiPresenter extends MvpBasePresenter<WifiInterface> {

    private final String TAG=WifiPresenter.class.getSimpleName();
    private WifiManager wifiManager;
    ArrayList<ScanResult> list;    //存放周围wifi热点对象的列表
    List<WifiBean> wifiBeanList;

    private Context context;

    private String wifiName;  //wifi名字
    private String macAddress;  //mac地址
    private int rssi;  //rssi的值
    private String apName;  //AP名字

    private CompositeDisposable compositeDisposable;

    public WifiPresenter(Context mContext) {
        this.context = mContext;
        wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        compositeDisposable=new CompositeDisposable();
    }


    //开启WIFI模块功能
    public void openWiFi(){
        if(wifiManager.isWifiEnabled()){
           getInfo();
        }else{
            wifiManager.setWifiEnabled(true);
//        if (wifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLING){
//            getView().changeButtonText("WIFI正在开启 *");
//            Log.i("wifi开启状态", "openWIFI: "+wifiManager.getWifiState());
//        }else if (wifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED){
//            getView().changeButtonText("WIFI已开启 *");
//            Log.i("wifi开启状态", "openWIFI: "+wifiManager.getWifiState());
//        }
//        getView().changeButtonText("WIFI已开启 *");
        }
        Log.i(TAG, "openWIFI: "+wifiManager.isWifiEnabled());
//        getInfo();
    }


    /**
     * WiFi是否已被打开
     */
    public boolean  checkWifi(){
        if(wifiManager.isWifiEnabled()){
            getInfo();
            return true;
        }
        return false;
    }

    WifiInfo info;
    /**
     * 获取WiFi名称、强度、Mac地址、AP地址
     */
    public void getInfo(){
        Log.i(TAG, "openWIFI: "+wifiManager.isWifiEnabled());
        if(!wifiManager.isWifiEnabled()){
            wifiManager.setWifiEnabled(true);
            getView().changeButtonText("1");
            return;
        }
        wifiManager.startScan();  //开始扫描AP
        Observable.timer(2,TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(Long aLong) {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        info = wifiManager.getConnectionInfo();
                        list = (ArrayList<ScanResult>) wifiManager.getScanResults();
                        Log.i(TAG, "附近wifi数量: "+list.size());
                        if(list.size()==0){
                            getView().changeButtonText("1");
                        }else{
                            wifiBeanList = new ArrayList<>();
                            for(int i=0;i<list.size();i++) {
                                wifiName = list.get(i).SSID;
                                rssi = list.get(i).level;
                                macAddress = info.getMacAddress();
                                String top = "WiFi名称：" + wifiName + "   RSSI：" + rssi + "";
                                String bottom = "MAC地址：" + list.get(i).BSSID + "";
                                wifiBeanList.add(new WifiBean(R.mipmap.wifi_logo, top, bottom));
                            }
                            getView().showWIFI(wifiBeanList);
                        }
                    }
                });

    }

    public void closeWIFI(){
        compositeDisposable.clear();
        if (wifiManager!=null) {
            wifiManager.setWifiEnabled(false);
            Log.i(TAG, "closeWIFI: " + wifiManager.isWifiEnabled());
            Log.i(TAG, "openWIFI: "+wifiManager.getWifiState());
        }
        if (wifiBeanList!=null)
        wifiBeanList.clear();
    }
}
